package main

import (
	"github.com/devfeel/dotweb"
	"strconv"
	"git.oschina.net/YPTArch/usercenter/libs"
	_"git.oschina.net/YPTArch/usercenter/remoteserver/rpcremote"
	"git.oschina.net/YPTArch/usercenter/controllers"
	"git.oschina.net/YPTArch/usercenter/config"

	"git.oschina.net/YPTArch/basic/sys/logs"
	"git.oschina.net/YPTArch/usercenter/oauth2/server"
	"git.oschina.net/YPTArch/usercenter/libs/data"
)
func init() {
/*	//先起日志，然后起配置
	logs.NewLog()
	//配置自己实例化
	config.Conf = new(config.Config)
	if err := config.LoadConfig(); err != nil {
		os.Exit(1)
	}
	fmt.Println(config.Conf.DB)*/
}

func main() {
	//rpcremote.RunRPCClient()

	StartServer()
}
func StartServer() error {
	app := dotweb.New()
	//设置dotserver日志目录
	app.SetLogPath(fileutil.GetCurrentDirectory()+"/logs/")
	//app.SetDevelopmentMode()
	//设置gzip开关
	app.HttpServer.SetEnabledGzip(true)
	app.HttpServer.Features.SetEnabledCROS()
	app.SetExceptionHandle(func(ctx dotweb.Context,err error) {
			logs.Logger.Info(err.Error())
			result := new(data.Result)
			result.ServerErrResult()
			ctx.WriteJson(result)
	})

	//设置路由
	InitRoute(app.HttpServer)
	TempRoute(app.HttpServer)
	//使用OAuth2
	server.UseOAuth2(app)
	logs.Logger.Info("dotweb.OAuth2.Register success ")
	// 开始服务
	port,err:=strconv.Atoi(config.GetHttpPort())
	if err != nil {
		logs.Logger.Error("端口未配置")
		return err
	}
	logs.Logger.Info("dotweb.StartServer => " + strconv.Itoa(port))
	err = app.StartServer(port)
	logs.Logger.Info("dotweb.StartServer error => "+ err.Error())
	return err
}

func InitRoute(httpServer *dotweb.HttpServer) {
	httpServer.Router().POST("/usercenter/getuser",controllers.GetUserById)

	httpServer.Router().POST("/usercenter/resetpwd",controllers.ResetUserPwd)
	httpServer.Router().POST("/usercenter/updatepwd",controllers.UpdateUserPwd)
	httpServer.Router().POST("/usercenter/updatephone",controllers.UpdateUserPhone)
	httpServer.Router().POST("/usercenter/updateemail",controllers.UpdateUserEmail)
	httpServer.Router().POST("/usercenter/updatename",controllers.UpdateUserName)

	httpServer.Router().POST("/usercenter/register", controllers.CreateAndAuthorization)

}

func TempRoute(httpServer *dotweb.HttpServer)  {
	httpServer.Router().Any("/usercenter/loginout", func(ctx dotweb.Context) error {
			r:=new(data.Result)
			r.SuccessResult("")
			ctx.WriteJson(r)
		return nil
	})
}
